﻿using System;
using System.Collections.Generic;
using System.Text;

using YAMM.PlugIn;

namespace Browser
{
    public class Plugin : IPlugin
    {
        IServiceProvider serviceProvider = null;

        #region IPlugin Members

        public string Name
        {
            get { return "Web Browser"; }
        }

        public Guid Guid
        {
            get { return new Guid("B19B1C6F-7B21-4645-925C-BDD6093DE572"); }
        }

        public string Version
        {
            get { return "0.0.1"; }
        }

        public string Author
        {
            get { return "Christian Schmiljun"; }
        }

        public string Description
        {
            // TODO
            get { return ""; }
        }

        public System.Drawing.Image Icon
        {
            get { return (System.Drawing.Image)global::Browser.Properties.Resources.ResourceManager.GetObject("Web"); }
        }

        public void Configure()
        {
            throw new NotImplementedException();
        }

        public bool CanReceive(Type t)
        {
            throw new NotImplementedException();
        }

        public void Receive(object o)
        {
            throw new NotImplementedException();
        }

        public void Load(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;

            
            // create Browserwindow
            IWindowManager windowManager = serviceProvider.GetService(typeof(IWindowManager)) as IWindowManager;
            global::Browser.BrowserControl browserCtrl = new BrowserControl();
            IWindow browser = windowManager.Items.Add(Guid.NewGuid(), "Browser", browserCtrl);
            browser.Visible = false;
            //browser.Visible = true;
            //browserCtrl.webBrowser1.Navigate("https://x2chris-dev.heimnetz:8081/projects/heimnetz/wiki/YAMM");
                
            // create Menu
            IToolStripMenuManager menuManager = serviceProvider.GetService(typeof(IToolStripMenuManager)) as IToolStripMenuManager;
            IToolStripMenu menu = menuManager.Items.AddToolBar(Guid.NewGuid(), "Browser");                                
            // Buttons, ... in eigenem Menu
            IToolStripButton back = menu.Items.AddButton(Guid.NewGuid(), "Zurück",
                delegate(object sender, EventArgs e)
                {
                    browserCtrl.webBrowser1.GoBack();
                    browser.Visible = true;
                }
            );
            IToolStripButton forward = menu.Items.AddButton(Guid.NewGuid(), "Vorwärts", 
                delegate(object sender, EventArgs e)
                {
                    browserCtrl.webBrowser1.GoForward();
                    browser.Visible = true;
                }
            );
            menu.Items.AddSeparator(Guid.NewGuid());
            menu.Items.AddButton(Guid.NewGuid(), "Adresse: ", null).Enabled = false;
            IToolStripComboBox adressComboBox = menu.Items.AddComboBox(Guid.NewGuid(), null);
            adressComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
            IToolStripButton load = menu.Items.AddButton(Guid.NewGuid(),
                "Laden",
                delegate(object sender, EventArgs e)
                {
                    if (!String.IsNullOrEmpty(adressComboBox.Text))
                    {
                        browserCtrl.webBrowser1.Navigate(adressComboBox.Text);
                        browser.Visible = true;
                    }
                },
                ((System.Drawing.Icon)global::Browser.Properties.Resources.ResourceManager.GetObject("load")).ToBitmap()
            );
            menu.Visible = false;
            adressComboBox.Items.AddRange(new string[] { "www.codeplex.com/YAMM", "www.codeplex.com", "www.google.de", "https://x2chris-dev.heimnetz:8081" });
            adressComboBox.Width = 200;

            // Button im MainMenu -> Extra
            IToolStripDropDownButton config = menuManager.Items[StandardIdentifiers.MainMenu].Items[StandardIdentifiers.MainMenuExtra] as IToolStripDropDownButton;
            IToolStripCheckableButton b = 
                config.Items.AddCheckableButton(
                    Guid.NewGuid(),
                    "Browser",
                    delegate(object sender, EventArgs e)
                    {
                        if (((IToolStripCheckableButton)sender).Checked == true)
                        {
                            menu.Visible = true;
                            browser.Visible = true;
                        }
                        else
                        {
                            menu.Visible = false;
                            browser.Visible = false;
                        }
                    },
                    false,
                    Icon
                ); 

            // Menu verstecken wenn Browser nicht sichtbar
            browser.Hidden +=
                delegate(object sender, EventArgs e)
                {
                    b.Checked = false;
                    menu.Visible = false;
                };

        }

        public void Unload()
        {
            throw new NotImplementedException();
        }

        public void Uninstall()
        {
           
        }

        public bool IsConfigureable()
        {
            return false;
        }

        public event DataEventHandler Send;

        #endregion
    }
}
